<?php

namespace app\cms\validate;

use app\common\validate\BaseValidate;
class Warehouse extends BaseValidate
{
    // 基础规则
    protected $rule = [
        'id'           => 'require|integer|gt:0',
        'ids'          => 'require|array|checkIds',
        'shelfno'      => 'require|between:1,99',
        'layerno'      => 'integer|between:1,4',
        'layertype'    => 'integer|in:1,2,3',
        'total_layers' => 'require|between:1,10',
        'area'         => 'require|in:1,2', // 1:顺丰 2:京东
        'is_locked'    => 'require|in:0,1'   // 0:开启 1:禁用
    ];

    // 错误消息
    protected $message = [
        'id.require'           => 'ID必须是正整数',
        'id.integer'           => 'ID必须是正整数',
        'id.gt'                => 'ID必须是正整数',
        'area.require'         => '暂时只开通了顺丰京东',
        'area.in'              => '暂时只开通了顺丰京东',
        'shelfno.require'      => '货架号不能为空',
        'shelfno.between'      => '货架号需1-99之间',
        'layerno.integer'      => '层数必须是整数',
        'layerno.between'      => '层数必须在1-4之间',
        'layertype.integer'    => '货架只有小、中、大件层',
        'layertype.in'         => '货架只有小、中、大件层',
        'total_layers.require' => '层数不能为空',
        'total_layers.between' => '层数只能在1-10层之间',
        'is_locked.require'    => '状态不能为空',
        'is_locked.in'         => '状态只能为开启或禁用',
        'ids.require'          => 'IDS列表不能为空',
        'ids.array'            => 'ID列表必须是数组',

    ];

    // 自定义验证规则 - 检查IDs数组
    protected function checkIds($value)
    {
        if (empty($value)) return 'ID列表不能为空';

        foreach ($value as $id) {
            if (!is_numeric($id) || $id <= 0) {
                return 'ID列表包含无效值';
            }
        }
        return true;
    }


    // 批量删除场景验证
    public function sceneBatchDelete()
    {
        return $this->only(['ids']);
    }

    // 场景配置
    protected $scene = [
        'create' => ['shelfno', 'total_layers', 'area'], // 创建货架
        'createone' => ['area', 'shelfno', 'layerno', 'layertype', 'is_locked'],//创一个货架层
        'switch' => ['id', 'is_locked'],                  // 切换状态
        'update' => ['id', 'area', 'shelfno', 'layerno', 'layertype', 'max_num', 'is_locked'] //更新场景验证
    ];

}